package com.elitel.common.auth.sysresource.controller;


import com.elitel.common.auth.sysresource.entity.SysResource;
import com.elitel.common.auth.sysresource.properties.SysResourceConfigProperties;
import com.elitel.common.auth.sysresource.service.SysResourceService;
import com.elitel.common.core.domain.JsonResult;
import com.elitel.common.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

import static com.elitel.common.core.domain.JsonResult.success;

/**
 * 功能描述
 *
 * @author: mls
 */

@RestController("sysResourceController")
@RequestMapping("menu")
public class SysResourceController {



    private static final Logger logger = LoggerFactory.getLogger(SysResourceController.class);

    @Resource
    private SysResourceConfigProperties sysResourceConfigProperties;

    @GetMapping("list")
    public JsonResult list() {
        Object bean = SpringUtils.getBean(sysResourceConfigProperties.getBeanName());
        if (bean instanceof SysResourceService) {
            SysResourceService sysResourceService = (SysResourceService) bean;
            try {
                List<SysResource> sysResourceList = sysResourceService.getMenuList();
                return success(sysResourceList);
            } catch (Exception e) {
                logger.error("获取菜单失败", e);
                return JsonResult.error(e.getMessage());
            }
        } else {
            return JsonResult.error("获取beanName为" + sysResourceConfigProperties.getBeanName() + "的bean失败，请检查MenuConfigProperties配置的beanName和实现类beanName是否一致");
        }

    }

    @GetMapping("update")
    public JsonResult update() {
        Object bean = SpringUtils.getBean(sysResourceConfigProperties.getBeanName());
        if (bean instanceof SysResourceService) {
            SysResourceService sysResourceService = (SysResourceService) bean;
            try {
                sysResourceService.updateMenuList();
                return success();
            } catch (Exception e) {
                logger.error("获取菜单失败", e);
                return JsonResult.error(e.getMessage());
            }
        } else {
            return JsonResult.error("获取beanName为" + sysResourceConfigProperties.getBeanName() + "的bean失败，请检查MenuConfigProperties配置的beanName和实现类beanName是否一致");
        }
    }
}
